package com.timk.goserver.server.services;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.timk.goserver.client.model.ClientUserInfo;
import com.timk.goserver.client.services.NoSessionException;
import com.timk.goserver.client.services.UserService;
import com.timk.goserver.server.model.ServerUserInfo;
import com.timk.goserver.server.util.GSLogger;
import com.timk.goserver.server.util.HibernateUtil;

/**
 * User service implementation
 * @author TKington
 *
 */
public class UserServiceImpl extends AbstractServiceServlet implements UserService {
	public ClientUserInfo getCurrentUser() throws NoSessionException {
		return getUser(getCurrentUsername());
	}

	public ClientUserInfo getUser(String username) throws NoSessionException {
		//	Make sure session is valid
		getSession();
		
		ServerUserInfo sUser = ServerUserInfo.findUser(username, null);
		
		if(sUser == null) {
			return null;
		}
		
		return sUser.toClientUserInfo();
	}

	public void setUserInfo(ClientUserInfo info) throws NoSessionException {
		String curUser = getCurrentUsername();
		
		Session hbSession = HibernateUtil.startTrans();
		
		ServerUserInfo sUser = ServerUserInfo.findUser(curUser, hbSession);
		
		sUser.setFullname(info.getFullname());
		sUser.setEmail(info.getEmail());
		sUser.setNotification(info.getNotification());
		sUser.setImageSize(info.getImageSize());
		
		hbSession.getTransaction().commit();
	}

	public int changePassword(String oldPassword, String newPassword)
			throws NoSessionException {
		String curUser = getCurrentUsername();
		Session hbSession = HibernateUtil.startTrans();
		
		ServerUserInfo sUser = ServerUserInfo.findUser(curUser, hbSession);
		
		try {
			if(sUser.checkPassword(oldPassword)) {
				sUser.updatePassword(newPassword);
			} else {
				return STATUS_INCORRECT_PASSWORD;
			}
		} catch (Exception e) {
			GSLogger.logError("Error changing password", e);
			return STATUS_INTERNAL_ERROR;
		}
		
		
		hbSession.getTransaction().commit();
		
		return STATUS_SUCCESS;
	}

	public List getUsers(int firstResult, int maxResults, String substring)
			throws NoSessionException {
		//	Make sure session is valid
		getSession();
		
		Session hbSession = HibernateUtil.startTrans();
		
		Query query = null;
		
		if(substring.length() > 0) {
			query = hbSession.createQuery("from ServerUserInfo as user where " +
					"user.username like '%" + substring + "%'" +
					" or user.fullname like '%" + substring + 
					"%' order by user.username");
		} else {
			query = hbSession.createQuery("from ServerUserInfo order by username");
		}
		
		query.setFirstResult(firstResult);
		query.setMaxResults(maxResults);
		
		List result = query.list();
		
		hbSession.getTransaction().commit();

		ArrayList list = new ArrayList();
		for (Iterator iter = result.iterator(); iter.hasNext();) {
			ServerUserInfo user = (ServerUserInfo) iter.next();
			list.add(user.toClientUserInfo());
		}
		return list;
	}
}
